package com.example.dingzexi_homework1;

import android.content.Intent;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
    private Context context;
    private String[] items;
    private int[] imageResources = {
            R.raw.image1, R.raw.image2, R.raw.image3, R.raw.image4
    };
    private String[] captions = {
            "武汉大学生黄鹤楼打卡攻略，快艾特你的姐妹来种草吧。", "做个好男友，学会这十六道菜，轻松拿捏女朋友的胃。", "逆天室友真下头，集美们评价一下。", "Csgo游戏梗图合集。"
    };
    public ListAdapter(Context context, String[] items, int[] imageResources) {
        this.context = context;
        this.items = items;
        this.imageResources = imageResources;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.imageView.setImageResource(imageResources[position]);
        holder.textView.setText(captions[position]); // 设置图片下面的文字

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, DetailActivity.class);
            intent.putExtra("image_res_id", imageResources[position]);
            intent.putExtra("caption", captions[position]);
            context.startActivity(intent);
        });
    }
    @Override
    public int getItemCount() {
        return items.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.item_image);
            textView = view.findViewById(R.id.item_text);
        }
    }

}